package com.example.wasaimovie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wasaimovie.Annotation.DisableBaseResponse;
import com.example.wasaimovie.Model.entity.Admin;
import com.example.wasaimovie.Model.support.ResponseResult;
import com.example.wasaimovie.Service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Value("${movie.image.url.path}")
    String url;

    /**
     * 管理员登录
     * @param admin 输入的管理员账号密码
     * @return 是否成功
     */
    @PostMapping("/login")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> AdminLogin(@RequestBody Admin admin){
        Map<String, Object> map = adminService.adminLogin(admin);
        if (map.isEmpty()){
            // 用户名或密码错误
            return new ResponseResult<>(400, null);
        }
        return new ResponseResult<>(map);
    }

    /**
     * 重置密码
     * @param username 用户名
     * @return 重置是否成功
     */
    @PostMapping("/forget")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> resetPwd(String username){
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("username", username);
        List<Admin> list = adminService.list(qw);
        if (list.isEmpty()){
            // 查不到用户
            return new ResponseResult<>(400, "用户名不存在");
        }
        Admin admin = list.get(0);
        admin.setPassword("123456");
        adminService.updateById(admin);
        return new ResponseResult<>(200, null);
    }

    /**
     * 获取管理员信息
     * @param id 管理员id
     * @return 返回管理员信息
     */
    @GetMapping("/{id}")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> getAdminInfo(@PathVariable Integer id){
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("id", id);
        List<Admin> list = adminService.list(qw);
        if (list.isEmpty()){
            return new ResponseResult<>(400, "该用户不存在");
        }
        Admin admin = list.get(0);
        admin.setAvatar(url + admin.getAvatar());
        return new ResponseResult<>(admin);
    }

    @PostMapping("/update")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> update(@RequestBody Admin admin){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        admin.setUpdateTime(format.format(new Date()));
        admin.setAvatar(admin.getAvatar().substring(admin.getAvatar().lastIndexOf('/') + 1));
        if (adminService.updateById(admin)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "修改失败");
    }

}
